package org.net
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.LoaderContext;
	
	import org.abcFrame.core.*;
	import org.enmu.*;
	import mx.managers.CursorManager;
	import flash.system.ApplicationDomain;
	
	public class AbcLoaderText
	{
		private var path:String
		private var responder:AbcResponder
		/**
		 * 加载文本方法 
		 * @param path 文件路径
		 * @param responder 异步响应捕捉类
		 * @param value 要传递的参数，默认值为null
		 */
		public function AbcLoaderText(path:String,responder:AbcResponder,value:XML=null)
		{
			this.path=path
			this.responder=responder
				
			CursorManager.setBusyCursor();
			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.TEXT;
			configureListeners(loader);
			var request:URLRequest = new URLRequest(path);
			if(value!=null){
				var variables:URLVariables = new URLVariables();
				variables.data=value;
				request.data = variables;
				request.method = URLRequestMethod.POST;
			}
			try {
				loader.load(request);
			} catch (error:Error) {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"loader:加载"+path+"出问题了===="+error.message)
			}
			
		}
		
		function configureListeners(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		function completeHandler(event:Event):void {
			try {
				//trace(URLLoader(event.target).data)
				if(responder.onResult!=null)responder.onResult(URLLoader(event.target).data,path);
			} catch (error:Error) {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"completeHandler:处理"+path+"返回结果的responder函数出错了,可能是参数或是逻辑错误！"+error.message)
			}
			
			CursorManager.removeAllCursors()
		}
		
		function openHandler(event:Event):void {
			AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"openHandler:"+event.toString())
		}
		
		function progressHandler(event:ProgressEvent):void {
			if(responder.onProgress!=null)responder.onProgress(event.bytesLoaded/event.bytesTotal);
		}
		
		function securityErrorHandler(event:SecurityErrorEvent):void {
			AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"securityErrorHandler:加载"+path+"出问题了===="+event.text)
		}
		
		function httpStatusHandler(event:HTTPStatusEvent):void {
			AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"httpStatusHandler:"+event.toString())
		}
		
		function ioErrorHandler(event:IOErrorEvent):void {
			AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"ioErrorHandler:加载"+path+"出问题了===="+event.text)
		}
		
	}
}

